package edu.pet.Servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import edu.pet.DAO.PetDAO;
import edu.pet.entity.Owner;

public class OwnerUpdateServlet extends HttpServlet {

	public OwnerUpdateServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String msg = "";
		
		String handle = request.getParameter("handle");
		String ownername = request.getParameter("ownername") == null ? "" : request.getParameter("ownername");
		String address = request.getParameter("address") == null ? "" : request.getParameter("address");
		String city = request.getParameter("city") == null ? "" : request.getParameter("city");
		String telephone = request.getParameter("telephone") == null ? "" : request.getParameter("telephone");
		
		Owner owner = new Owner();
		
		owner.setName(ownername);
		owner.setAddress(address);
		owner.setCity(city);
		owner.setTelephone(telephone);
		
		PetDAO dao = new PetDAO();
		HttpSession session = request.getSession();
		
		System.out.println(handle + ownername + address + city + telephone);
		if(handle.equals("update")){
			int ownerid = Integer.parseInt(request.getParameter("ownerid"));
			System.out.println(ownerid);
			
			owner.setOwner_id(ownerid);
			if(dao.updateOwner(owner)){
				msg="修改成功";
				session.setAttribute("owner", owner);
			}else{
				msg="修改失败";
			}
			session.setAttribute("msg",msg);
			response.sendRedirect("../user/ownerinfo.jsp");
		}else{
			if(dao.addOwner(owner)){
				msg = "添加成功！";
			}else{
				msg = "添加失败";
			}
			session.setAttribute("msg",msg);
			response.sendRedirect("../user/petsearch.jsp");
		}
			
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

	public void init() throws ServletException {
	}

}
